package com.snimay.app.clazz;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.snimay.app.clazz.repository.ClazzRepository;
import com.snimay.app.clazz.vo.Clazz;
import com.snimay.common.RestServiceController;
import com.snimay.freemarker.FreemarkerUtil;
import com.snimay.javacompiler.compiler.JavaFile;
import com.snimay.util.ToWeb;

@RestController
@RequestMapping("clazz")
public class ClazzController  implements RestServiceController<Clazz,Long>{
	@Autowired
	ClazzRepository repository;
	@Autowired
	FreemarkerUtil freemarkerUtil;
	
	@Autowired
	JavaFile javaFile; 
	@Override
	public Object getOne(@PathVariable("id") Long id) {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(repository.findOne(id));
		return new Gson().toJson(toweb);
	}
	@GetMapping("/")
	public Object getList() {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(repository.findAll());
		return new Gson().toJson(toweb);
	}
	
	@GetMapping("/name/{name}")
	public Object getOne(@PathVariable("name") String name) {
		ToWeb toweb = new ToWeb();
		toweb.setObjData(repository.findByName(name));
		return new Gson().toJson(toweb);
	}

	@GetMapping("/id/{id}")
	public Object getOneBy(@PathVariable("id") Long id) {
		ToWeb toweb = new ToWeb();
		Gson gs	=	new Gson();
		Clazz clazz = repository.findById(id);
		//toweb.setObjData(clazz);
		Map<String,Object> mp = gs.fromJson(gs.toJson(clazz),Map.class);
		String s =freemarkerUtil.getString("Entity.java", mp);
		javaFile.setFile(s);
		toweb.setObjData(s);
		//System.out.println(s);
		return new Gson().toJson(toweb);
	}
	
	@Override
	public Object getList(Integer rowSize, Integer page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@PostMapping(produces="text/plain;charset=UTF-8")
	public Object postOne(@RequestBody Clazz entity) {
		entity = repository.save(entity);
		ToWeb toweb = new ToWeb();
		toweb.setObjData(entity);
		return new Gson().toJson(toweb);
	}

	@Override
	@PutMapping(produces="text/plain;charset=UTF-8")
	public Object putOne(Long id,@RequestBody Clazz entity) {
		//repository.
		
		repository.save(entity);
		ToWeb toweb = new ToWeb();
		toweb.setObjData(entity);
		return new Gson().toJson(toweb);
	}

	@Override
	public Object patchOne(Long id, Clazz entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@DeleteMapping("/{id}")
	public Object deleteOne(@PathVariable("id") Long id) {
		ToWeb toweb = new ToWeb();
		repository.delete(id);
		return new Gson().toJson(toweb);
	}
}
